﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Msung.Core.Extensions
{
    public enum TimeType
    {
        /// <summary>
        /// 秒
        /// </summary>
        Seconds = 0,
        /// <summary>
        /// 分钟
        /// </summary>
        Minutes = 1,
        /// <summary>
        /// 小时
        /// </summary>
        Hours = 2,
        /// <summary>
        /// 天
        /// </summary>
        Days = 3,
        /// <summary>
        /// 月
        /// </summary>
        Months = 4
    }
    public  static class DateTimeExtension
    {
        /// <summary>
        /// 获取时间相差天数
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static int DateCalculation(this DateTime startTime, DateTime endTime, TimeType? timeType = TimeType.Days)
        {
            try
            {
                double result = 0;
                TimeSpan Start = new TimeSpan(startTime.Ticks);
                TimeSpan End = new TimeSpan(endTime.Ticks);
                TimeSpan ts = Start.Subtract(End).Duration();
                switch (timeType)
                {

                    case TimeType.Seconds:
                        result = ts.TotalSeconds;
                        break;
                    case TimeType.Minutes:
                        result = ts.TotalMinutes;
                        break;
                    case TimeType.Hours:
                        result = ts.TotalHours;
                        break;
                    case TimeType.Days:
                        result = ts.TotalDays;
                        break;
                    case TimeType.Months:
                        int Year = startTime.Year - endTime.Year;
                        result = (endTime.Year - startTime.Year) * 12 + (endTime.Month - startTime.Month);
                        break;
                }
                if (result < 0)
                    result = result * -1;
                return Convert.ToInt32(result);
            }
            catch
            {
                return 0;
            }
        }
    }
}
